﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Net.Http.Formatting;

namespace PivotData.Tests
{
    [TestClass]
    public class FacetNumberTest
    {
        [TestMethod]
        public void TestEqualNumber()
        {
            var f1 = new FacetNumber() { Name = "1", Number = 2 };
            var f2 = new FacetNumber() { Name = "1", Number = 2 };

            Assert.IsTrue(f1.Equals(f2), "equal");
        }

        [TestMethod]
        public void TestNEqualNumber()
        {
            var f1 = new FacetNumber() { Name = "1", Number = 2 };
            var f2 = new FacetNumber() { Name = "2", Number = 2 };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestNEqualNumberV()
        {
            var f1 = new FacetNumber() { Name = "1", Number = 2 };
            var f2 = new FacetNumber() { Name = "1", Number = 1 };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestNEqualNumberOtherObj()
        {
            var f1 = new FacetNumber() { Name = "1", Number = 2 };
            var f2 = new FacetString() { Name = "1", Value = "10" };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestJsonRoundTripNumberConversionJson()
        {
            var fsOrig = new FacetNumber() { Name = "hi", Number = 10.3 };
            var formatter = new JsonMediaTypeFormatter();
            var stream = new MemoryStream();

            formatter.WriteToStreamAsync(typeof(FacetAbsBase[]), new FacetAbsBase[] { fsOrig }, stream, content: null, transportContext: null).Wait();
            stream.Position = 0;
            var roundtrippedError = formatter.ReadFromStreamAsync(typeof(FacetAbsBase[]), stream, content: null, formatterLogger: null).Result as FacetAbsBase[];

            Assert.IsNotNull(roundtrippedError);
            Assert.AreEqual(1, roundtrippedError.Length);
            Assert.IsInstanceOfType(roundtrippedError[0], typeof(FacetNumber));
            Assert.AreEqual("hi", roundtrippedError[0].Name, "name of fs number");
            Assert.AreEqual(10.3, (roundtrippedError[0] as FacetNumber).Number, "value of fs number");
        }
    }
}
